package user_team

import (
	"data/config"
	"data/view"
	"public/mysqldb"
)

//获取团队基本信息
func OnGetTeamBasic(name string, id int) (team view.Team_info_tbl) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	if len(name) > 0 {
		orm.Where("name = ? ", name).Find(&team)
	}
	if id > 0 {
		orm.First(&team, id)
	}
	return
}

//用户添加团队
func OnUserAddOneTeam(user string, team_id int) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var u_t view.User_team_tbl
	u_t.User = user
	u_t.Team_ref_id = team_id
	if orm.Create(&u_t).Error != nil {
		return false
	} else {
		return true
	}
}

//用户添加多个团队
func OnUserAddMultiTeam(user string, team_name []string) (b bool) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())
	//删除存在的
	orm.Table("user_team_tbl").Where("user = ?", user).Delete(&view.User_team_tbl{})

	if len(team_name) > 0 {
		for _, v := range team_name {
			t_id := OnGetTeamBasic(v, 0).Id
			if t_id == 0 {
				b = false
				return
			}
			if !OnUserAddOneTeam(user, t_id) {
				b = false
				return
			}
		}
		b = true
	}
	return

}
